local Utility = {}
local U = require "packages/utility/utility"
Utility.cardDic = {
  {"slash", Card.Club, 4},
  {"jink", Card.Diamond, 2},
  {"peach", Card.Heart, 6},
  {"analeptic", Card.Spade, 9},
  {"fire__slash", Card.Heart, 4},
  {"thunder__slash", Card.Spade, 4},
  {"ex_nihilo", Card.Heart, 8},
  {"amazing_grace", Card.Heart, 4},
  {"god_salvation", Card.Heart, 1},
  {"dismantlement", Card.Spade, 3},
  {"snatch", Card.Diamond, 3},
  {"duel", Card.Diamond, 1},
  {"fire_attack", Card.Diamond, 2},
  {"savage_assault", Card.Spade, 13},
  {"archery_attack", Card.Heart, 1},
  {"collateral", Card.Club, 13},
  {"duel", Card.Diamond, 1},
  {"iron_chain", Card.Club, 12},
  {"nullification", Card.Heart, 13},
  {"indulgence", Card.Heart, 6},
  {"supply_shortage", Card.Spade, 10},
  {"lightning", Card.Heart, 12},

  {"crossbow", Card.Diamond, 1},
  {"qinggang_sword", Card.Spade, 6},
  {"ice_sword", Card.Spade, 2},
  {"double_swords", Card.Spade, 2},
  {"blade", Card.Spade, 5},
  {"spear", Card.Spade, 12},
  {"axe", Card.Diamond, 5},
  {"halberd", Card.Diamond, 12},
  {"kylin_bow", Card.Heart, 5},
  {"eight_diagram", Card.Spade, 2},
  {"nioh_shield", Card.Club, 2},
  {"dilu", Card.Club, 5},
  {"jueying", Card.Spade, 5},
  {"zhuahuangfeidian", Card.Heart, 13},
  {"chitu", Card.Heart, 5},
  {"dayuan", Card.Spade, 13},
  {"zixing", Card.Diamond, 13},
  {"guding_blade", Card.Spade, 1},
  {"fan", Card.Diamond, 1},
  {"vine", Card.Spade, 2},
  {"silver_lion", Card.Club, 1},
  {"hualiu", Card.Diamond, 13},
}
Utility.resetExtraCardPile = function (room)
  U.prepareDeriveCards(room, Utility.cardDic, "ExtraCardPile_reset")
    for _, id in ipairs(room:getTag("ExtraCardPile_reset")) do
      room:setCardMark(Fk:getCardById(id), "@@ExtraCard_Mark", 1)
    end
    room:setBanner("@$ExtraCardPile", room:getTag("ExtraCardPile_reset"))
end
Utility.getExtraCardPile = function (room)
  return room:getTag("ExtraCardPile")
end
Utility.getExtraCardById = function (room, player, ids)
  if player.dead then return {} end
  if type(ids) == "number" then
    ids = {ids}
  end
  local cids = {}
  for _, id in ipairs(ids) do
    if table.contains(Utility.getExtraCardPile(room), id) then
      table.insertIfNeed(cids, id)
    end
  end
  if #cids > 0 then
    --[[local event_data = {
      to = player,
      toArea = Player.Hand,
      cards = cids
    }]]
    room:moveCardTo(cids, Card.PlayerHand, player, fk.ReasonJustMove)
    --room.logic:trigger("fk.AfterExtraCardMoveOut", player, event_data)
  end
  return {}
end
Utility.getExtraCardByName = function (room, player, name, isTrueName, number)
  number = number or 999
  if player.dead or number <= 0 then return {} end
  local cids = table.filter(Utility.getExtraCardPile(room), function (id)
    local c = Fk:getCardById(id)
    if isTrueName then
      return c.trueName == name
    else
      return c.name == name
    end
  end)
  if #cids > 0 then
    if #cids > number then
      cids = table.random(cids, number)
    end
    --[[local event_data = {
      to = player,
      toArea = Player.Hand,
      cards = cids
    }]]
    room:moveCardTo(cids, Card.PlayerHand, player, fk.ReasonJustMove)
    --room.logic:trigger("fk.AfterExtraCardMoveOut", player, event_data)
  end
  return {}
end
Utility.putCardsIntoExtraPile = function (room, ids, proposer)
  if type(ids) == "number" then
    ids = {ids}
  end
  room:moveCardTo(ids, Card.Void, nil, fk.ReasonJustMove, nil, nil, proposer.id)
end
return Utility